package com.adcc.monitoragent.core;

import java.util.Date;

import org.apache.log4j.PropertyConfigurator;
import org.quartz.DateBuilder;
import org.quartz.Job;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.SimpleScheduleBuilder;
import org.quartz.SimpleTrigger;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.adcc.monitoragent.core.service.DatabaseService;
import com.adcc.monitoragent.core.service.MongoService;
import com.adcc.monitoragent.core.service.MqService;
import com.adcc.monitoragent.core.service.OsService;
import com.adcc.monitoragent.core.service.RedisService;
import com.adcc.monitoragent.core.service.ZookeeperService;
import com.adcc.monitoragent.log.ServiceLogger;
import com.adcc.monitoragent.send.MessageSend;
import com.adcc.monitoragent.util.ProtertiesUtil;

public class MainCore {

	private final static Logger log = LoggerFactory.getLogger(MainCore.class);
	/**
	 * 创建定时任务
	 * @param jobClass
	 * @param sched
	 * @param jobName
	 * @param triggerName
	 * @param group
	 */
	private static void createJob(Class<? extends Job> jobClass, Scheduler sched,
			String jobName, String triggerName, String group)  {
		try {
			Date startTime = DateBuilder.nextGivenSecondDate(null, 1);
			JobDetail job = JobBuilder.newJob(jobClass)
					.withIdentity(jobName, group).build();

			SimpleTrigger trigger = TriggerBuilder
					.newTrigger()
					.withIdentity(triggerName, group)
					.startAt(startTime)
					.withSchedule(
							SimpleScheduleBuilder
									.simpleSchedule()
									.withIntervalInSeconds(
											ProtertiesUtil.getTimeInterval())
									.repeatForever()).build();

			Date ft = sched.scheduleJob(job, trigger);
			log.debug(job.getKey() + " will run at: " + ft + " and repeat: "
					+ trigger.getRepeatCount() + " times, every "
					+ trigger.getRepeatInterval() / 1000 + " seconds");
		} catch (SchedulerException e) {
			log.error("定时任务创建失败" + jobName + e.getMessage());
		}
	}
	
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		//配置log4j
		PropertyConfigurator.configure("conf/log4j.properties");
		//设置依赖库路径
		System.setProperty("java.library.path", System.getProperty("user.dir"));
		//初始化属性工具参数
		ProtertiesUtil.init();
		//传输连接初始化
		MessageSend.init();
		//初始化日志
		ServiceLogger.init(ProtertiesUtil.getOsIp(), "", "MonitorAgent");
		
		// 监控数据库，redis，mq等定时任务，同时往外发送广播信息
		// First we must get a reference to a scheduler
		try {
			SchedulerFactory sf = new StdSchedulerFactory();
			Scheduler sched = sf.getScheduler();
			// 创建定时任务
			createJob(OsService.class, sched, "job1", "trigger1", "group1");
			createJob(DatabaseService.class, sched, "job2", "trigger2", "group2");
			createJob(MongoService.class, sched, "job3", "trigger3", "group3");
			createJob(MqService.class, sched, "job4", "trigger4", "group4");
			createJob(RedisService.class, sched, "job5", "trigger5", "group5");
			createJob(ZookeeperService.class, sched, "job6", "trigger6", "group6");
			sched.start();
		} catch (SchedulerException e) {
			log.error("定时任务创建失败" + e.getMessage());
		}
	}

}
